//
//  FriendCell.swift
//  MoyoungDevice
//
//  Created by 易先生 on 2022/8/26.
//

import UIKit
import EarbudsDatabase

class FriendCell: TableViewCell {

	override func makeUI() {
		super.makeUI()

		contentView.addSubview(headImage)
		contentView.addSubview(titleLabel)
		contentView.addSubview(phoneLabel)
		contentView.addSubview(iconImage)

		headImage.snp.makeConstraints { make in
			make.leading.equalToSuperview().offset(20)
			make.centerY.equalToSuperview()
			make.size.equalTo(CGSize(width: 40, height: 40))
		}

		iconImage.snp.makeConstraints { make in
			make.trailing.equalToSuperview().offset(-20)
			make.centerY.equalToSuperview()
			make.size.equalTo(CGSize(width: 22, height: 22))
		}

		phoneLabel.snp.makeConstraints { make in
            make.trailing.equalTo(iconImage.snp.leading).offset(-10)
			make.centerY.equalToSuperview()
			make.width.equalTo(100)
		}

		titleLabel.snp.makeConstraints { make in
            make.leading.equalTo(headImage.snp.trailing).offset(10)
			make.trailing.equalTo(phoneLabel.snp.leading).offset(-10)
			make.top.equalToSuperview().offset(20)
			make.bottom.equalToSuperview().offset(-20)
		}
	}

	public func configModel(model: ContactModel) {
		self.titleLabel.text = model.fullName
		self.phoneLabel.text = model.selectPhoneNumber
        if let imageData = model.imageData {
            self.headImage.image = UIImage(data: imageData)
        } else {
            if let avatar = UIImage().createAvatarWith(name: model.fullName, color: R.color.settings_1_contacts_bg() ?? UIColor.black) {
                self.headImage.image = avatar
            }
        }
	}

	private lazy var headImage: ImageView = {
		let headImage = ImageView()
		headImage.contentMode = .scaleAspectFit
		headImage.layer.cornerRadius = 20
		headImage.layer.masksToBounds = true
		return headImage
	}()

	private lazy var titleLabel: Label = {
		let titleLabel = Label()
        titleLabel.textColor = R.color.common_3_text()
		titleLabel.font = Fonts.systemFont(ofSize: 16)
		titleLabel.numberOfLines = 0
		return titleLabel
	}()

	private lazy var phoneLabel: Label = {
		let phoneLabel = Label()
        phoneLabel.textColor = R.color.common_23_text_second()
        phoneLabel.font = Fonts.systemFont(ofSize: 14)
		return phoneLabel
	}()

	private lazy var iconImage: UIImageView = {
		let iconImage = UIImageView()
        iconImage.image = R.image.ic_common_edit()
		return iconImage
	}()
}
